package com.yuandian.business.monitor.controller;

import cn.hutool.core.util.ObjectUtil;
import com.yuandian.api.business_monitor.po.AlarmParams;
import com.yuandian.api.business_monitor.po.QueryBusinessOverviewParams;
import com.yuandian.api.business_monitor.po.SystemOverviewParams;
import com.yuandian.api.management.feign.RemoteOptionService;
import com.yuandian.api.management.po.OptionParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.IBusinessOverviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 说明：业务概况controller
 *
 * @Author zfsn
 * @Date 2023/2/6 16:46
 */
@RestController
@RequestMapping("/business/overview")
@Api(value = "businessOverview", tags = "业务概况接口")
public class BusinessOverviewController {

	@Autowired
	private RemoteOptionService remoteOptionService;
	@Autowired
	private IBusinessOverviewService businessOverviewService;

	@ApiOperation(value = "查询业务概况列表", notes = "查询业务概况列表")
	@GetMapping("/list")
	public R list() {
		return R.ok(remoteOptionService.system(new OptionParams()));
	}


	/**
	 * 查询全系统总览
	 *
	 * @param params
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序规则（DESC降序、ASC升序）", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "查询全系统总览", notes = "查询全系统总览")
	@PostMapping("/querySystemOverview")
	public R querySystemOverview(SystemOverviewParams params) {

		return R.ok(businessOverviewService.querySystemOverview(params));
	}

	/**
	 * 分页查询业务概况列表
	 *
	 * @param params 业务概况查询参数
	 * @return 通用返回对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodeList", 			value = "业务编码", 			paramType = "query", 	required = false, 	dataType = "Array"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序规则（DESC降序、ASC升序）", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "查询业务概况列表", notes = "查询业务概况列表")
	@PostMapping("/queryBusinessOverviewListPage")
	public R queryBusinessOverviewListPage(@RequestBody QueryBusinessOverviewParams params) {
		return R.ok(businessOverviewService.queryBusinessOverviewListPage(params));
	}

	/**
	 * 查询步骤列表Excel
	 *
	 * @param params 业务概况查询参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导出步骤列表", notes = "导出步骤列表")
	@PostMapping("/exportStepList")
	public void exportStepList(@RequestBody QueryBusinessOverviewParams params, HttpServletResponse response) {
		try {
			businessOverviewService.exportStepList(params, response);
		} catch (Exception e) {
			e.printStackTrace();
			R.failed("业务概况导出失败");
		}

	}

	/**
	 * 导出业务概况列表Excel文件
	 *
	 * @param params 业务概况查询参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导出业务概况列表", notes = "导出业务概况列表")
	@PostMapping("/exportBusinessOverviewList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodeList", 			value = "业务编码", 			paramType = "query", 	required = false, 	dataType = "Array"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序规则（DESC降序、ASC升序）", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	public void exportBusinessOverviewList(@RequestBody QueryBusinessOverviewParams params, HttpServletResponse response) {
		try {
			businessOverviewService.exportBusinessOverviewList(params, response);
		} catch (Exception e) {
			e.printStackTrace();
			R.failed("业务概况导出失败");
		}
	}


	/**
	 * 导出业务概况列表Excel文件   导出的字段与上面不一致
	 *
	 * @param params 业务概况查询参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "查询业务概况列表(导出字段不一样)", notes = "查询业务概况列表（导出字段不一样）")
	@PostMapping("/exportBusinessOverviewListCopy")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodeList", 			value = "业务编码", 			paramType = "query", 	required = false, 	dataType = "Array"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序规则（DESC降序、ASC升序）", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	public void exportBusinessOverviewListCopy(@RequestBody QueryBusinessOverviewParams params, HttpServletResponse response) {
		try {
			businessOverviewService.exportBusinessOverviewListCopy(params, response);
		} catch (Exception e) {
			e.printStackTrace();
			R.failed("业务概况导出失败");
		}
	}


	/**
	 * 根据系统ID、业务编码和时间范围查询业务健康度、成功率等环形图数据信息
	 *
	 * @param params
	 * @return 返回通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "环形图数据信息接口", notes = "环形图数据信息接口")
	@PostMapping("/getBuinessHealthAndSuccessRatePercen")
	public R getBuinessHealthAndSuccessRatePercen(@RequestBody QueryBusinessOverviewParams params) {
		return R.ok(businessOverviewService.getBuinessPercen(params));
	}


	/**
	 * 获取总转换率和访问量
	 *
	 * @param params
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "获取总转换率和访问量", notes = "获取总转换率和访问量")
	@PostMapping("/getConversionRate")
	public R getConversionRate(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.getConversionRate(params);
	}

	/**
	 * 查询业务健康度、访问量、平均响应时长（业务监控详情页折线数据源）
	 *
	 * @param params 查询参数
	 * @return 业务健康度、访问量、平均响应时长结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "业务监控详情页：业务健康度、访问量、平均响应时长", notes = "业务监控详情页：业务健康度、访问量、平均响应时长")
	@PostMapping("/getBuinessLineChartData")
	public R getBuinessLineChartData(@RequestBody QueryBusinessOverviewParams params) {
		return R.ok(businessOverviewService.getBuinessLineChartData(params));
	}

	/**
	 * 根据系统ID、业务编码、时间范围查询相应的步骤分析信息
	 *
	 * @param params 查询参数
	 * @return 通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "查询步骤分析信息", notes = "查询步骤分析信息")
	@PostMapping("/getBuinessProcessAnalysis")
	public R getBuinessProcessAnalysis(@RequestBody QueryBusinessOverviewParams params) {
		if (params == null || params.getSystemId() == null || ObjectUtil.isEmpty(params.getBusinessCode())) {
			return R.failed("请求参数异常");
		}
		R buinessProcessAnalysis = businessOverviewService.getBuinessProcessAnalysis(params);
		return R.ok(buinessProcessAnalysis);
	}

	/**
	 * 根据系统ID、业务编码、时间范围查询步骤趋势分析数据
	 *
	 * @param params 查询参数（系统ID、业务编码、时间范围）
	 * @return 通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "查询步骤趋势分析数据", notes = "查询步骤趋势分析数据")
	@PostMapping("/getBuinessProcessTrendData")
	public R getBuinessProcessTrendData(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.getBuinessProcessTrendData(params);
	}

	/**
	 * 根据系统ID、业务编码、时间范围查询网段详细信息数据
	 *
	 * @param params 查询参数（系统ID、业务编码、时间范围）
	 * @return 通用结果集
	 */
	@ApiOperation(value = "查询网端详细信息数据", notes = "查询网端详细信息数据")
	@PostMapping("/getSegmentDetailList")
	public R getSegmentDetailList(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.getSegmentDetailList(params);
	}


	/**
	 * 根据系统ID、业务编码、时间范围查询it路径图
	 *
	 * @param params 查询参数（系统ID、业务编码、时间范围）
	 * @return 通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
	})
	@ApiOperation(value = "查询it路径图", notes = "查询it路径图")
	@PostMapping("/stepEndToEndRelation")
	public R stepEndToEndRelation(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.stepEndToEndRelation(params);
	}


	/**
	 * 新增网端坐标
	 *
	 * @param params
	 * @return 通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "locationX", value = "坐标x", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "locationY", value = "坐标y", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "protocolCode", value = "协议编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "网端名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "网端类型", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "sign", value = "端标识，0：非首尾端(默认)、1：首端、2：尾端", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isContinue", value = "是否与上一端联系，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isBroadcast", value = "是否广播，0：否(默认网络数据交互类型)、1：是(只做转发网络数据内容不变)", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isNat", value = "是否NAT，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isKey", value = "是否为关键网端，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isFoundByFront", value = "是否通过前一端发现当前端IP，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer")
	})
	@ApiOperation(value = "新增网端坐标", notes = "新增网端坐标")
	@PostMapping("/addSegmentPosition")
	public R addSegmentPosition(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.addSegmentPosition(params);
	}

	/**
	 * 更新网端坐标
	 *
	 * @param params
	 * @return 通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "locationX", value = "坐标x", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "locationY", value = "坐标y", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "protocolCode", value = "协议编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "网端名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "网端类型", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "sign", value = "端标识，0：非首尾端(默认)、1：首端、2：尾端", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isContinue", value = "是否与上一端联系，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isBroadcast", value = "是否广播，0：否(默认网络数据交互类型)、1：是(只做转发网络数据内容不变)", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isNat", value = "是否NAT，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isKey", value = "是否为关键网端，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isFoundByFront", value = "是否通过前一端发现当前端IP，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer")
	})
	@ApiOperation(value = "更新网端坐标", notes = "更新网端坐标")
	@PostMapping("/updateSegmentPosition")
	public R updateSegmentPosition(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.updateSegmentPosition(params);
	}

	/**
	 * 业务步骤统计列表
	 *
	 * @param params
	 * @return 通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "业务步骤统计列表", notes = "业务步骤统计列表")
	@PostMapping("/getBusiStepStatList")
	public R getBusiStepStatList(@RequestBody QueryBusinessOverviewParams params) {
		return businessOverviewService.getBusiStepStatList(params);
	}

	/**
	 * 按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)
	 *
	 * @param params 查询参数
	 * @return 返回通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCode", 			value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "percentType", 			value = "0-告警类型占比  1-告警等级占比", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = false, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation(value = "按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)")
	@PostMapping("/getBusinessMonitorAlarmPercent")
	public R getBusinessMonitorAlarmPercent(@RequestBody AlarmParams params) {
		return R.ok(businessOverviewService.getBusinessMonitorAlarmPercent(params));
	}


}
